package com.hibase.upms.service.dict.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.upms.entity.dict.MdsDict;
import com.hibase.upms.entity.dict.vo.MdsDictSearchVO;
import com.hibase.upms.mapper.dict.MdsDictMapper;
import com.hibase.upms.service.dict.MdsDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 字典主表 服务实现类
 * </p>
 *
 * @author chenfeng
 * @since 2021-03-12
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class MdsDictServiceImpl extends BaseServiceImpl<MdsDictMapper, MdsDict> implements MdsDictService {

    /**
     * 查询字典主表列表
     */
    @Override
    public PageInfo<MdsDict> selectMdsDictList(MdsDictSearchVO searchVO) {
    	super.pageMaps(searchVO, null); 
        return super.page(searchVO, new QueryWrapper<MdsDict>()
                .like(StrUtil.isNotBlank(searchVO.getDictType()),"dict_type",searchVO.getDictType()));
    }

    /**
     * 改变字典主表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMdsDict(MdsDict entity) {
        super.store(entity);
    }

    /**
     * 查询字典主表详情
     */
    @Override
    public MdsDict viewMdsDict(String id) {

        MdsDict entity = super.getById(id, true);

        return entity;
    }

    /**
     * 删除字典主表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMdsDict(String id) {
        super.deleteBy(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void enableMdsDict(List<String> ids) {
        Collection<MdsDict> dictList = super.listByIds(ids);
        dictList.forEach(dict ->{
            HibaseAssert.isFalse(dict.getBeActive(),"请勿重复操作");
            dict.setBeActive(Boolean.TRUE);
        });
        super.storeBatch(dictList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disableMdsDict(List<String> ids) {
        Collection<MdsDict> dictList = super.listByIds(ids);
        dictList.forEach(dict ->{
            HibaseAssert.isTrue(dict.getBeActive(),"请勿重复操作");
            dict.setBeActive(Boolean.FALSE);
        });
        super.storeBatch(dictList);
    }
}
